package top.yudoge.dogeeditor.llm.rewrite

import org.springframework.stereotype.Component

@Component
class UserInputAnalyseChainNode : TaskChainNode {

    override fun process(ctx: ChainContext) {

        if (ctx.query.code.isNullOrBlank()) {
            putChainStore(ctx, "tables", listOf<String>())
            emitNext(ctx)
            return
        }

        sendReasoningCallback(ctx, "Analyzing user code")

        var prompt = loadPrompt(
            "/extract_table_name.template", mapOf(
                "user_code" to ctx.query.code,
                "user_task" to ctx.query.userTask
            )
        )

        var response = ctx.chatModel.chat(prompt)
        var tableName = safeReadJsonArray(response, String::class.java)

        putChainStore(ctx, TABLE_NAME_KEY, tableName)
        emitNext(ctx)
    }

    companion object {
        val TABLE_NAME_KEY = "table_name"
    }

}